package at.irian.jsfatwork.broker.domain.order;

import at.irian.jsfatwork.broker.domain.client.Client;
import at.irian.jsfatwork.broker.domain.stock.Stock;

import javax.persistence.Transient;
import java.math.BigDecimal;

/**
 * @author Thomas Spiegl
 */
public class Order {

    private OrderType orderType;

    private OrderStatus orderStatus;

    private Stock stock;

    private BigDecimal limit;

    private BigDecimal completionPrice;

    private Integer quantity;

    private Client client;

    /**
     * Protected access - must be created by Ordering only
     */
    protected Order(OrderType orderType, OrderStatus orderStatus, Stock stock, BigDecimal limit, Integer quantity, Client client) {
        this.orderType = orderType;
        this.orderStatus = orderStatus;
        this.stock = stock;
        this.limit = limit;
        this.quantity = quantity;
        this.client = client;
    }

    public Stock getStock() {
        return stock;
    }

    public OrderType getOrderType() {
        return orderType;
    }

    public BigDecimal getLimit() {
        return limit;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Client getClient() {
        return client;
    }

    public OrderStatus getOrderStatus() {
        return orderStatus;
    }

    public BigDecimal getCompletionPrice() {
        return completionPrice;
    }


    // ----------- transient getters and logic --------------


    public void processOrderResponse(OrderResponse orderResponse) {
        this.orderStatus = orderResponse.getOrderStatus();
        this.completionPrice = orderResponse.getCompletionPrice();
    }

    @Transient
    public boolean isComplete() {
        return this.orderStatus == OrderStatus.COMPLETE;
    }
}
